# Load required libraries
library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.3.3
## Warning: package 'tidyr' was built under R version 4.3.3
## Warning: package 'readr' was built under R version 4.3.3
## Warning: package 'dplyr' was built under R version 4.3.3
## Warning: package 'stringr' was built under R version 4.3.3
## Warning: package 'lubridate' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.0 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(spData)
## Warning: package 'spData' was built under R version 4.3.3
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
library(sf)
## Warning: package 'sf' was built under R version 4.3.3
## Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
library(mapview)
## Warning: package 'mapview' was built under R version 4.3.3
library(foreach)
##
## Attaching package: 'foreach'
##
## The following objects are masked from 'package:purrr':
##
## accumulate, when
library(doParallel)
## Warning: package 'doParallel' was built under R version 4.3.3
## Loading required package: iterators
## Loading required package: parallel
library(tidycensus)
## Warning: package 'tidycensus' was built under R version 4.3.3
# Register parallel backend
registerDoParallel(4)
# Set Census API key
census_api_key("a097fa58a6b68a54c5d80f082378c6feaec23c7d")
## To install your API key for use in future sessions, run this function with `install = TRUE`.
#Defining variables to the data
race_vars <- c(
"Total Population" = "P1_001N",
"White alone" = "P1_003N",
"Black or African American alone" = "P1_004N",
"American Indian and Alaska Native alone" = "P1_005N",
"Asian alone" = "P1_006N",
"Native Hawaiian and Other Pacific Islander alone" = "P1_007N",
"Some Other Race alone" = "P1_008N",
"Two or More Races" = "P1_009N"
)
options(tigris_use_cache = TRUE)
erie <- get_decennial(
geography = "block",
variables = race_vars,
year = 2020,
state = "NY",
county = "Erie County",
geometry = TRUE,
sumfile = "pl",
cache_table = TRUE
)
## Getting data from the 2020 decennial Census
## Using the PL 94-171 Redistricting Data Summary File
## Note: 2020 decennial Census data use differential privacy, a technique that
## introduces errors into data to preserve respondent confidentiality.
## ℹ Small counts should be interpreted with caution.
## ℹ See https://www.census.gov/library/fact-sheets/2021/protecting-the-confidentiality-of-the-2020-census-redistricting-data.html for additional guidance.
## This message is displayed once per session.
erie <- st_crop(erie,xmin=-78.9,xmax=-78.85,ymin=42.888,ymax=42.92)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
erie$variable <- factor(erie$variable,levels=unique(erie$variable))
race_points<-foreach(race=levels(erie$variable),.combine=rbind,.packages=c("sf", "dplyr")) %dopar% {
race_data<-erie %>% filter(variable==race)
sample_points<-st_sample(race_data,size=race_data$value,exact=TRUE)
sample_points_sf<-st_as_sf(sample_points) %>%
mutate(variable=race)
sample_points_sf
}
mapview(race_points, zcol="variable",legend=TRUE,cex=2.5)